package quanduoduo.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;

import quanduoduo.common.*;

public class IndexController extends Controller {
	static Log log = Log.getLog(IndexController.class);

	public void quan() {
		render("quan.jsp");
	}

	public void index() throws UnsupportedEncodingException {
		String q = getPara("q");
		if (q == null || q.isEmpty()) {
			render("index.jsp");
			return;
		}
		log.info(q);
		String itemid = findResult("https://item.taobao.com/item.htm[^\"]*id=(?<id>\\d+)", q, "id");
		if (itemid == null) {
			itemid = findResult("https://detail.m.tmall.com/item.htm[^\"]*id=(?<id>\\d+)", q, "id");
		}

		// 获取真实地址
		String sr = null;
		String redirecturl = null;
		if (itemid != null) {
			redirecturl = "http://weixin.quanduoduo.com/index/index?iid=" + itemid + "&q=" + q;
		} else {
			redirecturl = HttpRequest.getHeaderLocation("http://weixin.quanduoduo.com/index/search?q=" + q);
		}
		log.info("redirect url:" + redirecturl);
		sr = HttpRequest.sendGet(redirecturl);

		// 访问手淘地址，从返回内容中取得淘宝地址，然后取得itemid，再调用[http://weixin.quanduoduo.com/index/index]获取优惠券信息
		/*
		 * String st = findResult("(c.b0wr.com[^\"]*)",q); if ( st != null &&
		 * !st.isEmpty() && st.length() > 0 ){ log.info(st); String sret =
		 * HttpRequest.sendGet("http://"+st, null); if(sret !=null){ String
		 * realurl = findResult("(https://a.m.taobao.com[^']*)",sret); log.info(
		 * "real url:"+ realurl); String iid =
		 * findResult("(\\d[^.]*).htm",realurl); if ( iid != null ){ iid =
		 * iid.substring(0,iid.length()-4); log.info("iid:"+ iid); q= realurl;
		 * sr = HttpRequest.sendGet("http://weixin.quanduoduo.com/index/index",
		 * "iid="+ iid); }
		 * 
		 * } }else{ sr =
		 * HttpRequest.sendPost("http://weixin.quanduoduo.com/index/index",
		 * "q="+ q); }
		 */

		// log.info("content:"+sr);

		// 获取优惠券地址，直接跳转
		/*
		 * String retstr = findResult("(uland.taobao.com[^\"]*)", sr); retstr =
		 * URLDecoder.decode(retstr, "UTF-8"); log.info("uland url:"+ retstr);
		 * retstr = retstr.replaceAll("mm_\\d*_\\d*_\\d*",
		 * "mm_11297933_20274279_69532924"); retstr = "http://" + retstr;
		 * log.info(retstr); redirect(retstr);
		 */
		String mm = PropKit.get("alimama");
		String retstr = findResult("<div\\s+class=\\\"q-wx\\\">.*<\\/div>", sr);
		if (retstr != null) {
			String nogoods = findResult("<div\\s+class=\\\"q-none-goods\\\">.*<\\/div>", retstr);
			if (nogoods == null) {
				String qwx = retstr.replaceAll("mm_\\d*_\\d*_\\d*", mm);
				qwx = qwx.replaceAll("http://weixin.quanduoduo.com/index/quan?[^\"]*http", "http");
				qwx = URLDecoder.decode(qwx, "UTF-8");
				setAttr("qwx", qwx);
				render("quan.jsp");
				return;
			}
		}
		setAttr("qwx", 1);
		render("index.jsp");

	}

	public static String findResult(String regex, String beFoundString) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(beFoundString);
		if (matcher.find()) {
			return matcher.group();
		}
		return null;
	}

	public static String findResult(String regex, String beFoundString, String groupname) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(beFoundString);
		if (matcher.find()) {
			return matcher.group(groupname);
		}
		return null;
	}

}
